/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 29.07.2005
 *
 *************************************************/
package fightplan.views;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import fightplan.business.Tournament;
import fightplan.diag.TournamentOptionsDialog;
import fightplan.views.tables.DojoTable;

@SuppressWarnings("serial")
public class TournamentInfoPanel extends JPanel {
	private JTextField tName;
	private JTextField tPlace;
	private JTextField tDate;
	private JTextField tPersonTournament;
	private JTextField tPersonDrawing;
	private ValueModel tournament;

	public TournamentInfoPanel(ValueModel tournament) {
		this.tournament = tournament;

		setLayout(new BorderLayout());

		PropertyAdapter paName = new PropertyAdapter(tournament, "name");
		PropertyAdapter paPlace = new PropertyAdapter(tournament, "place");
		PropertyAdapter paDate = new PropertyAdapter(tournament, "date");
		PropertyAdapter paPersonTournament = new PropertyAdapter(tournament, "personTournament");
		PropertyAdapter paPersonDrawing = new PropertyAdapter(tournament, "personDrawing");

		tName = BasicComponentFactory.createTextField(paName);
		tPlace = BasicComponentFactory.createTextField(paPlace);
		tDate = BasicComponentFactory.createTextField(paDate);
		tPersonTournament = BasicComponentFactory.createTextField(paPersonTournament);
		tPersonDrawing = BasicComponentFactory.createTextField(paPersonDrawing);

		FormLayout layout = new FormLayout(
				"left:max(40dlu;p), 4dlu, pref:grow(1), 7dlu, left:max(40dlu;p), 4dlu, pref:grow(1)", // 1st
																										// major
																										// column
				// + "left:max(40dlu;p), 4dlu, pref:grow:fill, 1dlu, 20dlu", //
				// 2nd major column
				"20dlu, 20dlu, 20dlu, 20dlu, 20dlu, " + "20dlu, 60px:grow"); // add
																				// rows
																				// dynamically

		DefaultFormBuilder builder = new DefaultFormBuilder(layout);

		builder.setDefaultDialogBorder();
		builder.appendSeparator("Turnierinformationen");
		builder.append("Turniername", tName, 5);
		builder.nextLine();

		builder.append("Austragungsort", tPlace);
		builder.append("Wettkampf Verantwortlicher", tPersonTournament);
		builder.nextLine();

		builder.append("Datum", tDate);
		builder.append("Verantwortlich für die Losung", tPersonDrawing);
		builder.nextLine();

		builder.append(buildOptions(), 7);
		builder.nextLine();

		builder.appendSeparator("Dojos");
		builder.nextLine();

		builder.append(new DojoTable(tournament), 7);

		builder.nextLine();

		add(builder.getPanel(), BorderLayout.CENTER);
	}

	public Component buildOptions() {
		JPanel panel = new JPanel(new FlowLayout(SwingConstants.RIGHT));
		panel.add(new JButton(new OptionAction()));
		return panel;
	}

	private class OptionAction extends AbstractAction {

		public OptionAction() {
			putValue(Action.NAME, "Optionen...");
		}

		public void actionPerformed(ActionEvent e) {
			JFrame owner = (JFrame) SwingUtilities.getWindowAncestor(TournamentInfoPanel.this);
			TournamentOptionsDialog dialog = new TournamentOptionsDialog(owner, (Tournament) tournament.getValue());
			dialog.pack();
			dialog.setResizable(false);
			dialog.setLocationRelativeTo(owner);
			dialog.setVisible(true);
		}

	}

}
